// 数据源全局管理器
class DataManager {
    private static instance: DataManager;
    private terrainProviders: any[] = [];
    private imageryLayers: any[] = [];
    private dataSources: any[] = [];
    private entities: any[] = [];

    private constructor() {}

    public static getInstance(): DataManager {
        if (!DataManager.instance) {
            DataManager.instance = new DataManager();
        }
        return DataManager.instance;
    }

    // 添加地形服务
    public addTerrainProvider(terrainProvider: any): void {
        this.terrainProviders.push(terrainProvider);
        console.log(`地形服务已添加到管理器，当前数量: ${this.terrainProviders.length}`);
    }

    // 移除地形服务
    public removeTerrainProvider(terrainProvider: any, viewer: any): void {
        const index = this.terrainProviders.indexOf(terrainProvider);
        if (index > -1) {
            viewer.terrainProvider = new (window as any).Cesium.EllipsoidTerrainProvider();
            this.terrainProviders.splice(index, 1);
            console.log(`地形服务已从管理器移除，剩余数量: ${this.terrainProviders.length}`);
        }
    }

    // 添加影像图层
    public addImageryLayer(imageryLayer: any): void {
        this.imageryLayers.push(imageryLayer);
        console.log(`影像图层已添加到管理器，当前数量: ${this.imageryLayers.length}`);
    }

    // 移除影像图层
    public removeImageryLayer(imageryLayer: any, viewer: any): void {
        const index = this.imageryLayers.indexOf(imageryLayer);
        if (index > -1) {
            viewer.imageryLayers.remove(imageryLayer);
            this.imageryLayers.splice(index, 1);
            console.log(`影像图层已从管理器移除，剩余数量: ${this.imageryLayers.length}`);
        }
    }

    // 添加数据源
    public addDataSource(dataSource: any): void {
        this.dataSources.push(dataSource);
        console.log(`数据源已添加到管理器，当前数量: ${this.dataSources.length}`);
    }

    // 移除数据源
    public removeDataSource(dataSource: any, viewer: any): void {
        const index = this.dataSources.indexOf(dataSource);
        if (index > -1) {
            viewer.dataSources.remove(dataSource);
            this.dataSources.splice(index, 1);
            console.log(`数据源已从管理器移除，剩余数量: ${this.dataSources.length}`);
        }
    }

    // 添加实体
    public addEntity(entity: any): void {
        this.entities.push(entity);
        console.log(`实体已添加到管理器，当前数量: ${this.entities.length}`);
    }

    // 移除实体
    public removeEntity(entity: any, viewer: any): void {
        const index = this.entities.indexOf(entity);
        if (index > -1) {
            viewer.entities.remove(entity);
            this.entities.splice(index, 1);
            console.log(`实体已从管理器移除，剩余数量: ${this.entities.length}`);
        }
    }

    // 清除所有地形服务
    public clearAllTerrainProviders(viewer: any): void {
        console.log(`开始清除 ${this.terrainProviders.length} 个地形服务`);
        this.terrainProviders.forEach(terrainProvider => {
            viewer.terrainProvider = new (window as any).Cesium.EllipsoidTerrainProvider();
        });
        this.terrainProviders = [];
        console.log('所有地形服务已清除');
    }

    // 清除所有影像图层
    public clearAllImageryLayers(viewer: any): void {
        console.log(`开始清除 ${this.imageryLayers.length} 个影像图层`);
        this.imageryLayers.forEach(imageryLayer => {
            viewer.imageryLayers.remove(imageryLayer);
        });
        this.imageryLayers = [];
        console.log('所有影像图层已清除');
    }

    // 清除所有数据源
    public clearAllDataSources(viewer: any): void {
        console.log(`开始清除 ${this.dataSources.length} 个数据源`);
        this.dataSources.forEach(dataSource => {
            viewer.dataSources.remove(dataSource);
        });
        this.dataSources = [];
        console.log('所有数据源已清除');
    }

    // 清除所有实体
    public clearAllEntities(viewer: any): void {
        console.log(`开始清除 ${this.entities.length} 个实体`);
        this.entities.forEach(entity => {
            viewer.entities.remove(entity);
        });
        this.entities = [];
        console.log('所有实体已清除');
    }

    // 清除所有数据
    public clearAllData(viewer: any): void {
        this.clearAllTerrainProviders(viewer);
        this.clearAllImageryLayers(viewer);
        this.clearAllDataSources(viewer);
        this.clearAllEntities(viewer);
        console.log('所有数据已清除');
    }

    // 获取统计信息
    public getStatistics(): any {
        return {
            terrainProviders: this.terrainProviders.length,
            imageryLayers: this.imageryLayers.length,
            dataSources: this.dataSources.length,
            entities: this.entities.length,
            total: this.terrainProviders.length + this.imageryLayers.length + this.dataSources.length + this.entities.length
        };
    }

    // 获取所有管理的数据
    public getAllData(): any {
        return {
            terrainProviders: [...this.terrainProviders],
            imageryLayers: [...this.imageryLayers],
            dataSources: [...this.dataSources],
            entities: [...this.entities]
        };
    }
}

export default DataManager;
